<?php
import( 'de.ceus-media.file.ini.iniWriter' );
import( 'tools.Setup.interfaces.SetupScreen' );
import( 'tools.Setup.classes.BaseSetupScreen' );
class OptionScreen extends BaseSetupScreen implements SetupScreen
{
	public function handleAction( $action )
	{
		if( $action == "saveOptions" )
		{
			if( !file_exists( $_POST['jpg_path']."jpgraph.php" ) )
				throw new Exception( "JPGraph is not existing in path '".$_POST['jpg_path']."'" );
			$fileName	= $this->config['sources']['config'];
			if( !file_exists( $fileName ) )
				file_put_contents( $fileName, "" );
			$iw		= new IniWriter( $fileName, true );
			$iw->setProperty( 'default', $_POST['language'], 'languages' );
			$iw->setProperty( 'jpgraph', $_POST['jpg_path'], 'paths' );
			return true;
		}
	}

	public function getView()
	{
		$ir	= new IniReader( $this->config['sources']['config'], true );
		$config	= $ir->toArray( true );
	
		$ui["label_language"]	= "<label for='language'>".$this->words['fields']["language"]."</label>";
		$ui["label_jpg_path"]	= "<label for='jpg_path'>".$this->words['fields']["jpg_path"]."</label>";

		$languages	= explode( ",", $config['languages']['allowed'] );
		foreach( $languages as $language )
			$options[$language]	= $this->words['languages'][$language];
		$options['_selected']	= $config['languages']['default'];

		$ui["field_language"]	= Elements::Select( "language", $options );
		$ui["field_jpg_path"]	= Elements::Input( "jpg_path", $config['paths']['jpgraph'] );
		
		return $this->loadTemplate( $ui );
	}
}
?>